<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="bos-card">
      <!-- 添加列表区域 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAdd">添加分类</el-button>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <zk-table
        :data="cateList"
        :columns="cols"
        :expand-type="false"
        :selection-type="false"
        :show-index="true"
        index-text="#"
        border
      >
        <!-- 是否有效的  模板 -->
        <template #isOk="sc">
          <i
            style="color: red"
            v-if="sc.row.cat_deleted"
            class="el-icon-error"
          ></i>
          <i style="color: green" v-else class="el-icon-success"></i>
        </template>
        <!-- 级别模板 -->
        <template #level="sc">
          <el-tag v-if="sc.row.cat_level === 0">一级</el-tag>
          <el-tag v-else-if="sc.row.cat_level === 1" type="success"
            >二级</el-tag
          >
          <el-tag v-else type="warning">三级</el-tag>
        </template>
        <!-- 操作模板 -->
        <template #opt>
          <el-button size="mini" type="primary" icon="el-icon-edit"
            >编辑</el-button
          >
          <el-button size="mini" type="danger" icon="el-icon-delete"
            >删除</el-button
          >
        </template>
      </zk-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="querData.pagenum"
        :page-sizes="[1, 3, 5, 10, 15]"
        :page-size="querData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 新增分类 对话框 -->
    <el-dialog
      title="新增分类"
      :visible.sync="isShowAdd"
      width="50%"
      @close="closeAddDialog"
    >
      <!-- 新增表单 -->
      <el-form
        :model="addCateObj"
        :rules="addCateRules"
        ref="ruleForm"
        label-width="100px"
      >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addCateObj.cat_name"></el-input>
        </el-form-item>
        <!-- 父级分类 -->
        <el-form-item label="父级分类:">
          <el-cascader
            clearable
            v-model="selectedCateId"
            :options="parentCates"
            :props="{
              expandTrigger: 'hover',
              label: 'cat_name',
              value: 'cat_id',
            }"
            change-on-select
            @change="onParentChange"
          >
          </el-cascader>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="isShowAdd = false">取 消</el-button>
        <el-button type="primary" @click="doAddCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCatesAPI, addCateAPI } from "@/api";
export default {
  data() {
    return {
      querData: {
        pagenum: 1,
        pagesize: 2,
      },
      cols: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          type: "template",
          template: "isOk",
        },
        {
          label: "级别",
          type: "template",
          template: "level",
        },
        {
          label: "操作",
          type: "template",
          template: "opt",
        },
      ],
      cateList: [],
      total: 0,
      isShowAdd: false, // 新增分类的显示与隐藏
      //   新增分类 数据对象
      addCateObj: {
        cat_pid: 0,
        cat_name: "",
        cat_level: 0,
      },
      //   新增表单校验规则
      addCateRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
      //   一二级分类数组
      parentCates: [],
      selectedCateId: [],
    };
  },
  methods: {
    handleSizeChange(newSize) {
      this.querData.pagesize = newSize;
      this.getcateList();
    },
    handleCurrentChange(newPage) {
      this.querData.pagenum = newPage;
      this.getcateList();
    },
    //  获取 商品分类列表
    async getcateList() {
      const { data: res } = await getCatesAPI(this.querData);
      if (res.meta.status !== 200) return this.$msg.error(res.meta.msg);
      // 如果成功
      this.cateList = res.data.result;
      // console.log("hah--" + this.cateList);
      this.total = res.data.total;
    },
    // 显示新增对话框
    showAdd() {
      this.isShowAdd = true;
      this.getParentCates();
    },
    // 获取1,2 级分类数据
    async getParentCates() {
      const { data: res } = await getCatesAPI({ type: 2 });
      if (res.meta.status !== 200) return this.$msg.error(res.meta.msg);
      this.parentCates = res.data;
    },
    onParentChange() {
      //   console.log(this.selectedCateId);
      if (this.selectedCateId.length == 1) {
        this.addCateObj.cat_pid = this.selectedCateId[0];
      } else if (this.selectedCateId.length == 2) {
        this.addCateObj.cat_pid = this.selectedCateId[1];
      } else if (this.selectedCateId.length == 0) {
        this.addCateObj.cat_pid = 0;
      }
      this.addCateObj.cat_level = this.selectedCateId.length;
    },
    async doAddCate() {
      const { data: res } = await addCateAPI(this.addCateObj);
      if (res.meta.status !== 201) return this.$msg.error(res.meta.msg);
      this.isShowAdd = false;
    },
    // 关闭对话框
    closeAddDialog() {
      this.selectedCateId = [];
      this.$refs.ruleForm.resetFields();
    },
  },
  //   入口函数
  created() {
    this.getcateList();
  },
};
</script>

<style>
</style>